//
//  LoginViewController.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-15.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "PerfectContentViewController.h"

@interface LoginViewController ()<LoginViewDelegate>
{
    NSString *password;
}
@end

@implementation LoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    navigationBarView.title = @"登录";
    
    LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    [loginView setDelegate:self];
    [self.view addSubview:loginView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSString *user = [APP_USERDEFAULTS valueForKey:APP_LOGIN_SUCESS_SVAE_MSG];
    if (user) {
        //用户已登录
        [AppDelegate getAppDelegate].userMsg = [[UserMsg alloc] init:[user objectFromJSONString]];
        [self.navigationController pushViewController:[[AppDelegate getAppDelegate] getHomeVC] animated:NO];
    }
}

#pragma mark - LoginViewDelegate
-(void)login:(NSDictionary*)dic
{
    CLog(@"登录");
    password = [dic valueForKey:@"password"];
    [LoadingFrameView showLoadingview];
    ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_LOGIN_TAG need:YES];
    [self superASIStartAsynchronous:request];
}

-(void)pass
{
    CLog(@"忘记密码");
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    vc.viewType = Update_Type;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)registerUser
{
    CLog(@"注册");
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    [LoadingFrameView autoDisappearBlockingview:@"登录失败"];
}

//父类获取服务器返回信息，成功获取到数据执行方法
-(void)getASIRecordSucceed:(ASIHTTPRequest *)request data:(NSDictionary*)dic
{
    CLog(@"dic====%@",dic);
    [LoadingFrameView directlyDisappear];

    //保存信息到app
    UserMsg *user = [[UserMsg alloc] init:dic];
    APP_USER_MSG = user;
    APP_USER_MSG.password = password;
    
    if (user.nickname.length>0 && user.birthday.length > 0) {
        //保存用户信息在本地，下次自动登录
        NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:dic];
        [d setValue:password forKey:@"password"];
        [APP_USERDEFAULTS setValue:[d JSONString] forKey:APP_LOGIN_SUCESS_SVAE_MSG];
        
        [self.navigationController pushViewController:[[AppDelegate getAppDelegate] getHomeVC] animated:YES];
    }else{
        //完善个人资料
        [self.navigationController pushViewController:[[PerfectContentViewController alloc] init] animated:YES];
    }
}

//父类获取服务器返回信息，未成功获取到数据执行方法
-(void)getASIRecordFailed:(ASIHTTPRequest *)request data:(NSString*)failed
{
    [LoadingFrameView autoDisappearBlockingview:failed];
}

@end
